﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching;

using Husb.Data;
using Husb.DataUtil;

using StepTable = CentralOffice.Admin.Data.ApprovalStepDataSet.ApprovalStepDataTable;
using StepRow = CentralOffice.Admin.Data.ApprovalStepDataSet.ApprovalStepRow;


namespace CentralOffice.Admin.DataAccess
{
    public class ApprovalStepAdapter : DataAccessAdapter<Admin.Data.ApprovalStepDataSet, StepTable, StepRow>
    {
        private const string GetStepByProcessIdStoredProc = "Admin.ApprovalStep_SelectApprovalStepByProcessId"; 
        public ApprovalStepAdapter()
        {
            this.TableName = "ApprovalStep";

            GetAllStoredProc = "Admin.ApprovalStep_SelectApprovalStepsAll";
            InsertStoredProc = "Admin.ApprovalStep_InsertApprovalStep";
            UpdateStoredProc = "Admin.ApprovalStep_UpdateApprovalStep";
            DeleteStoredProc = "Admin.ApprovalStep_DeleteApprovalStep";
        }

        protected override void PopulateInsertParameters(Database db, DbCommand cmd)
        {
            db.AddInParameter(cmd, "Id", DbType.Guid, "Id", DataRowVersion.Current);
            db.AddInParameter(cmd, "ApprovalProcessId", DbType.Guid, "ApprovalProcessId", DataRowVersion.Current);
            db.AddInParameter(cmd, "Name", DbType.AnsiString, "Name", DataRowVersion.Current);
            db.AddInParameter(cmd, "StepOrder", DbType.Byte, "StepOrder", DataRowVersion.Current);
            db.AddInParameter(cmd, "PreviousStepId", DbType.Guid, "PreviousStepId", DataRowVersion.Current);
            db.AddInParameter(cmd, "ApprovalBy", DbType.AnsiString, "ApprovalBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "ApprovalContent", DbType.AnsiString, "ApprovalContent", DataRowVersion.Current);

            db.AddInParameter(cmd, "ApprovalByEmp", DbType.AnsiString, "ApprovalByEmp", DataRowVersion.Current);
            db.AddInParameter(cmd, "ExecuteCondition", DbType.AnsiString, "ExecuteCondition", DataRowVersion.Current);
            db.AddInParameter(cmd, "ExitCondition", DbType.AnsiString, "ExitCondition", DataRowVersion.Current);

            db.AddInParameter(cmd, "IsDeleted", DbType.Boolean, "IsDeleted", DataRowVersion.Current);
            db.AddInParameter(cmd, "IsActive", DbType.Boolean, "IsActive", DataRowVersion.Current);
            db.AddInParameter(cmd, "CreatedTime", DbType.DateTime, "CreatedTime", DataRowVersion.Current);
            db.AddInParameter(cmd, "CreatedBy", DbType.Guid, "CreatedBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "ModifiedTime", DbType.DateTime, "ModifiedTime", DataRowVersion.Current);
            db.AddInParameter(cmd, "LastModifiedBy", DbType.Guid, "LastModifiedBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "Version", DbType.Int32, "Version", DataRowVersion.Current);
            db.AddInParameter(cmd, "Description", DbType.String, "Description", DataRowVersion.Current);
        }

        public StepTable GetStepByProcessId(Guid processId)
        {
            List<DatabaseParameter> p = new List<DatabaseParameter>();
            p.Add(new DatabaseParameter("ApprovalProcessId", DbType.Guid, processId));

            StepTable steps = this.GetTable(GetStepByProcessIdStoredProc, p, true, false, null);

            return steps;
        }
    }
}
